import { createRouter, createWebHistory } from "vue-router";
import { getAccessToken } from '@/utils/auth';

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/login", 
			name: "login", 
			component: () => import("@/views/login/index.vue"),
			meta: { title: '登录', hidden: true, keys:'main' },
		},
		{
			path: "/home", 
			name: "home",
			component: () => import("@/views/index.vue"),
			meta: { title: '首页', icon: 'EachApp_shouye', keys:'main' },
		},
		{
			path: "/application",
			name: "application",
			component: () => import("@/views/application/index.vue"),
			meta: { title: '应用', icon: 'EachApp_yingyong', keys:'main' },
		},
		{
			path: "/userManage", 
			name: "userManage",
			component: () => import("@/views/userInfo/userManage/index.vue"),
			meta: { title: '设置', hidden: true, icon: 'EachApp_yonghu', keys:'main' },
		},
		{
			path: "/teamManage", 
			name: "teamManage",
			component: () => import("@/views/userInfo/teamManage/index.vue"),
			meta: { title: '组织管理', hidden: true, icon: 'EachApp_yonghu', keys:'main' },
		},
		{
			path: '/:catchAll(.*)',
			component: () => import('../components/mini.vue')//这个是我自己的路径
		},
	]
});

// 路由加载前
router.beforeEach(async (to, from, next) => {
	if (to.meta.title) {
		window.document.title = `Each-${to.meta.title}`;
	}
	if (getAccessToken()) {
		const is404 = to.matched?.filter(item => item.name !== '404');
		if (['/', '/login'].includes(to.path) || !is404.length) {
			next({ path: '/index' });
		} else {
			next();
		}
	} else {
		if (to.path === '/login') {
			next();
		} else {
			next(`/login`)
		}
	}
});

export default router;
